package com.ngkj.zmfptback.sys.initData.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.initData.bean.SysDict;
import com.ngkj.zmfptback.sys.initData.servicie.read.DictService;
import com.ngkj.zmfptback.sys.initData.servicie.write.DictWriteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ChenHN
 * @since 2021/10/29 creation
 **/
@RestController
public class DictController {

    @Resource
    private DictService dictService;

    @Resource
    private DictWriteService dictWriteService;

    @GetMapping("getCode")
    public CommonReslut<List<SysDict>> getSelectDicts(String dictType){
        List<SysDict> selectDicts = dictService.getSelectDicts(dictType);
        CommonReslut<List<SysDict>> listCommonReslut = new CommonReslut<>(ResultEnum.ok, selectDicts);
        return listCommonReslut;
    }

    @GetMapping("/dict/getDictPage")
    public CommonReslut getDictPage(SysDict sysDict,int pageNo,int pageSize){
        try {
            Page<SysDict> page = new Page<>(pageNo,pageSize);
            IPage<SysDict> sysDictIPage = dictService.getDictPage(page,sysDict);
            return new CommonReslut(ResultEnum.ok,sysDictIPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/dict/addDict")
    public CommonReslut addDict(@RequestBody SysDict sysDict){
        try {
            dictWriteService.addDict(sysDict);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/dict/editDict")
    public CommonReslut editDict(@RequestBody SysDict sysDict){
        try {
            dictWriteService.editDict(sysDict);
            return new CommonReslut<>(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @DeleteMapping("/dict/deleteDict")
    public CommonReslut deleteDict(Integer id){
        try {
            dictWriteService.deleteDict(id);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }


}
